package de.urlaub.client.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;

import de.urlaub.client.data.Vacation;
import de.urlaub.client.logic.VacationPanelUtil;
import de.urlaub.server.comm.ApprovalMailSender;
import de.urlaub.server.comm.ApprovalMailSenderRemote;
import de.urlaub.server.logic.DataUtil;
import de.urlaub.server.logic.DataUtilRemote;

public class RespondPanel extends JPanel 
{
	private DataUtilRemote dataUtil;
	private HashMap<Integer, Boolean> changedVacations;
	private JTable vacationTable;
	
	public RespondPanel(DataUtilRemote dataUtil)
	{
		this.dataUtil = dataUtil;
		try
		{
			Context context = VacationPanelUtil.getInitialContext();
			this.dataUtil = (DataUtilRemote) context.lookup("DataUtil/remote");
		}
		catch (NamingException nEx)
		{
			nEx.printStackTrace();
		}
		
		this.setName("Offene Anträge");
		this.setVisible(true);
	}
	
	@Override
	public String toString()
	{
		return this.getName();
	}
	
	public void buildPanel()
	{
		this.removeAll();
		this.setLayout(new BorderLayout());
		
		this.dataUtil.queryVacations(0);
		this.changedVacations = new HashMap<Integer, Boolean>();
		
		this.vacationTable = new JTable(this.dataUtil.getVacationTableData(true), 
										this.dataUtil.getVacationColumnNames(true));
		this.add(vacationTable, BorderLayout.CENTER);
		
		JPanel southPanel = new JPanel();
		JButton approveButton = new JButton("Genehmigen");
		approveButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				RespondPanel.this.changedVacations.put(RespondPanel.this.getSelectedVacationId(), true);
			}
		});
		southPanel.add(approveButton);
		
		JButton rejectButton = new JButton("Ablehnen");
		rejectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RespondPanel.this.changedVacations.put(RespondPanel.this.getSelectedVacationId(), false);
			}
		});
		southPanel.add(rejectButton);
		
		JButton commitButton = new JButton("Vorgang abschließen");
		commitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Context ctx = VacationPanelUtil.getInitialContext();
				DataUtilRemote dataUtil = RespondPanel.this.dataUtil;
				try
				{
					dataUtil = (DataUtilRemote) ctx.lookup("DataUtil/remote");
				}
				catch (NamingException nEx)
				{
					nEx.printStackTrace();
				}
				dataUtil.updateVacations(RespondPanel.this.changedVacations);
				for (Integer key : RespondPanel.this.changedVacations.keySet())
				{
					boolean isApproved = RespondPanel.this.changedVacations.get(key);
					if (isApproved)
					{
						ApprovalMailSenderRemote approvalMailSender = new ApprovalMailSender();
						try
						{
							approvalMailSender = (ApprovalMailSenderRemote) ctx.lookup("ApprovalMailSender/remote");
						}
						catch (NamingException nEx)
						{
							nEx.printStackTrace();
						}
						Vacation vacation = RespondPanel.this.dataUtil.getVacation(key);
						approvalMailSender.sendApprovalMail(vacation.getEmployee());
					}
				}
			}
		});
		southPanel.add(commitButton);
		
		this.add(southPanel, BorderLayout.SOUTH);
	}
	
	private Integer getSelectedVacationId()
	{
		int row = this.vacationTable.getSelectedRow();
		return (Integer) this.dataUtil.getVacationTableData(true)[row][0];
	}
	
	private Vacation getVacation(Integer vacationId)
	{	
		ArrayList<Vacation> vacations = this.dataUtil.getVacations();
		for (Vacation vacation : vacations) {
			if (vacation.getVacationId() == vacationId) {
				return vacation;
			}
		}
		return null;
	}
}
